package com.qp.bookkeeping.business.book.controller;

import com.qp.bookkeeping.business.book.model.req.DeleteBookReq;
import com.qp.bookkeeping.business.book.model.req.QueryBookReq;
import com.qp.bookkeeping.business.book.model.req.SaveBookReq;
import com.qp.bookkeeping.business.book.model.req.StatisticMonthReq;
import com.qp.bookkeeping.business.book.model.rsp.QueryBookRsp;
import com.qp.bookkeeping.business.book.model.rsp.StatisticMonthRsp;
import com.qp.bookkeeping.business.book.service.BookService;
import com.qp.bookkeeping.business.common.Exception.BusinessException;
import com.qp.bookkeeping.common.model.rsp.BaseRsp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xutianqi
 */
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    BookService bookService;

    @PostMapping("/bookkeeping")
    public <T> BaseRsp<T> bookkeeping(@RequestBody SaveBookReq bookkeepingReq) {
        bookService.bookkeeping(bookkeepingReq);
        return BaseRsp.success();
    }

    @PostMapping("/queryBook")
    public <T> BaseRsp<QueryBookRsp> queryBook(@RequestBody QueryBookReq queryBookReq) {
        QueryBookRsp queryBookRsp = bookService.queryBook(queryBookReq);
        return BaseRsp.success(queryBookRsp);
    }

    @PostMapping("/statisticMonth")
    public <T> BaseRsp<StatisticMonthRsp> statisticMonth(@RequestBody StatisticMonthReq statisticMonthReq) {
        StatisticMonthRsp statisticMonth = bookService.statisticMonth(statisticMonthReq);
        return BaseRsp.success(statisticMonth);
    }

    @PostMapping("/deleteBook")
    public <T> BaseRsp statisticMonth(@RequestBody DeleteBookReq deleteBookReq) {
        BaseRsp baseRsp = BaseRsp.success();
        try {
            bookService.deleteBookById(deleteBookReq);
        } catch (BusinessException e) {
            baseRsp.setRspCode(e.getCode());
            baseRsp.setRspMsg(e.getMessage());
        }
        return baseRsp;
    }
}
